import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:xindong/common/xindong_methods.dart';

// 全局方法 - 弹出alert
//
// builder
// context 上下文，为null时会获取全局currentContext
// barrierColor 蒙层阻挡色，默认 Colors.black54
// 基类弹窗Dialog
showAlertDialog({required Widget alert, Color barrierColor = Colors.black54, bool barrierDismissible = true}) async {
  return await showDialog(
    context: currentContext(),
    builder: (context) => alert,
    barrierColor: barrierColor,
    barrierDismissible: barrierDismissible,
    useSafeArea: false,
  );
}

showSmartAlert({required Widget alert, String? tag, Color? barrierColor, bool hideWhenTouchOutside = true, bool useAnimation = true}) {
  return SmartDialog.show(
    tag: tag,
    alignment: Alignment.center,
    backDismiss: hideWhenTouchOutside,
    clickMaskDismiss: hideWhenTouchOutside,
    maskColor: barrierColor,
    useAnimation: useAnimation,
    builder: (BuildContext context) => alert,
  );
}

class XinDongBaseDialog extends Dialog {
  final Widget? body; // 内容主体

  final bool hideWhenTouchOutside; // 触摸外部、返回键时，是否隐藏Dialog，默认true

  // 构造函数
  const XinDongBaseDialog({Key? key, this.hideWhenTouchOutside = true, this.body}) : super(key: key);

  // 确认调用方法
  void confirmAction(BuildContext context) {
    Navigator.pop(context, true);
  }

  // 取消调用方法
  void hideAction(BuildContext context) {
    Navigator.pop(context, null);
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      // 触摸外部、返回键时，是否隐藏Dialog
      onWillPop: () async => hideWhenTouchOutside,
      child: Material(
        type: MaterialType.transparency,
        child: Center(child: body),
      ),
    );
  }
}
